﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Model;
using EQT.Dal;
using System.Collections.Generic;

public partial class CreateNotice : System.Web.UI.Page
{
    private static readonly Common.Logging.ILog log = Common.Logging.LogManager.GetCurrentClassLogger();
    private DaNotice dalNotice = DevFuture.Common.WorkContext.AppCtx.GetObject("DaNotice") as DaNotice;
    private DaTfiles dalTfile = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTfiles") as DaTfiles;
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;

        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = true;

        if (!IsPostBack)
        {
            if (Request.QueryString["type"] == null) Response.End();
            if(Request.QueryString["type"]=="one")
            {
                DaTuser dal = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTuser") as DaTuser;
                CheckUsers.DataSource = dal.Query(" where status=2");
                CheckUsers.DataBind();
                user.Visible = true;
                ViewState.Add("TYPE",1);
                labType.Text = "定向消息";
                CreateSave.Visible = true;
                EditSave.Visible = false;
            }
            else if (Request.QueryString["type"] == "all")
            {
                labType.Text = "公告消息";
                user.Visible = false;
                ViewState.Add("TYPE", 0);
                CreateSave.Visible =true ;
                EditSave.Visible = false;
            }
            else
            { 
            //编辑
                if (Request.QueryString["noticeid"] != null)
                {
                    ViewState["NOTICEID"] = Request.QueryString["noticeid"];
                    CreateSave.Visible = false;
                    EditSave.Visible = true;
                    MoNotice mod = new MoNotice();
                    mod.NoticeId = Request.QueryString["noticeid"].ToString();
                     
                    if (dalNotice.GetEntityEx(ref mod))
                    {
                        labType.Text = mod.PublishType == 0 ? "公告消息" : "定向消息";
                        user.Visible = mod.PublishType == 0 ? false : true;
                        CreateNotice_Title.Text = mod.Title;

                        if (mod.PublishType == 1)
                        {
                            DaTuser dal = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTuser") as DaTuser;
                            CheckUsers.DataSource = dal.Query(" where status=2");
                            CheckUsers.DataBind();
                            
                            DaNoticeReceiver darec = DevFuture.Common.WorkContext.AppCtx.GetObject("DaNoticeReceiver") as DaNoticeReceiver;
                            IList<MoNoticeReceiver> lists= darec.GetEntities(" where notice_id='"+mod.NoticeId+"'");
                            if(lists!=null)
                            {
                                foreach(MoNoticeReceiver modr in lists)
                                {
                                    ListItem item=CheckUsers.Items.FindByValue(modr.Uid);
                                    if (item!=null)
                                    {
                                        item.Selected = true;
                                    }
                                }
                            }
                        }
                        string filename="NoticeFile//" + mod.NoticeId + ".htm";

                        filename = Server.MapPath(filename);
                        if (!string.IsNullOrEmpty(filename))
                        {
                            FCKeditorContent.Value = System.IO.File.ReadAllText(filename);
                        }
                    }
                }
            }

            FCKeditorContent.FormatOutput = true;
            FCKeditorContent.FormatSource = true;
        }
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。

        if (CreateNotice_Title.Text.Trim() == "")
        {
            OperationResult.Text = "请输入消息题目!"; divResult.Visible = true;
            return;
        }

        MoNotice mod = new MoNotice();

        try
        {
            mod.NoticeId = SequenceKeys.GenerateNoticeID();
            ViewState["NOTICE_ID"] = mod.NoticeId;
            mod.Uid = context.UserID;
            mod.PublishType = int.Parse(ViewState["TYPE"].ToString());
            mod.NoticeContent = mod.NoticeId;
            mod.CreateTime = DateTime.Now;
            mod.Title = CreateNotice_Title.Text.Trim();

            List<string> users = new List<string>();

            if (mod.PublishType == 0)
            {
                DaTuser daluser = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTuser") as DaTuser;
                DataSet ds = daluser.GetAllAvailableUsers();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    users.Add(ds.Tables[0].Rows[i]["UID"].ToString());
                }
            }
            else
            {
                for (int i = 0; i < CheckUsers.Items.Count; i++)
                {
                    if (CheckUsers.Items[i].Selected)
                    {
                        users.Add(CheckUsers.Items[i].Value);
                    }
                }

                if (users.Count < 1)
                {
                    OperationResult.Text = "请选择消息的接收人！"; divResult.Visible = true;
                    return;
                }
            }
            if (dalNotice.CreateNotice(mod, users))
            {
                Utility.SaveNoticeContent(mod.NoticeId, FCKeditorContent.Value);
                OperationResult.Text = "创建新消息成功!"; divResult.Visible = true;
                CreateSave.Enabled = false;
            }
            else
            {
                OperationResult.Text = "创建新消息失败!"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "创建新消息失败!"; divResult.Visible = true;
        }
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        if (CreateNotice_Title.Text.Trim() == "")
        {
            OperationResult.Text = "请输入消息题目!"; divResult.Visible = true;
            return;
        }

        MoNotice mod = new MoNotice();
        mod.NoticeId = ViewState["NOTICEID"].ToString();
        dalNotice.GetEntityEx(ref mod);

        try
        {
            mod.CreateTime = DateTime.Now;
            mod.Title = CreateNotice_Title.Text.Trim();

            List<string> users = new List<string>();

            if (mod.PublishType == 0)
            {
                DaTuser daluser = DevFuture.Common.WorkContext.AppCtx.GetObject("DaTuser") as DaTuser;
                DataSet ds = daluser.GetAllAvailableUsers();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    users.Add(ds.Tables[0].Rows[i]["UID"].ToString());
                }
            }
            else
            {
                for (int i = 0; i < CheckUsers.Items.Count; i++)
                {
                    if (CheckUsers.Items[i].Selected)
                    {
                        users.Add(CheckUsers.Items[i].Value);
                    }
                }

                if (users.Count < 1)
                {
                    OperationResult.Text = "请选择消息的接收人！"; divResult.Visible = true;
                    return;
                }
            }
            if (dalNotice.EditNoticeSave(mod, users))
            {
                Utility.SaveNoticeContent(mod.NoticeId, FCKeditorContent.Value);
                OperationResult.Text = "修改消息成功!"; divResult.Visible = true;
                EditSave.Enabled = false;
            }
            else
            {
                OperationResult.Text = "修改消息失败!"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "修改消息失败!"; divResult.Visible = true;
        }
    }

}
